<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>HTML5模拟微信聊天界面</title>
  <style>
    /**重置标签默认样式*/
    * {
      margin: 0;
      padding: 0;
      list-style: none;
      font-family: '微软雅黑';
    }

    html {
      height: 100%;
    }

    body {
      height: 100%;
    }

    #container {
      width: 450px;
      height: 100%;
      background: #eee;
      margin: 0 auto 0;
      top: 5%;
      position: relative;
      box-shadow: 20px 20px 55px #777;
      border-radius: 0 0 5px 5px;
    }

    .header {
      background: #000;
      height: 50px;
      color: #fff;
      line-height: 40px;
      font-size: 16px;
      padding: 0 10px;
      border-radius: 5px 5px 0 0;
    }

    .footer {
      width: 430px;
      height: 40px;
      background: #666;
      position: fixed;
      bottom: 0;
      padding: 10px;
      border-radius: 0 0 5px 5px;
      font-size: 16px;
    }

    .footer input {
      width: 285px;
      height: 45px;
      outline: none;
      font-size: 16px;
      text-indent: 10px;
      position: absolute;
      border-radius: 6px;
      right: 80px;
    }

    .footer span {
      display: inline-block;
      width: 62px;
      height: 48px;
      background: #ccc;
      font-weight: 900;
      line-height: 45px;
      cursor: pointer;
      text-align: center;
      position: absolute;
      right: 10px;
      border-radius: 6px;
    }

    .footer span:hover {
      color: #fff;
      background: #999;
    }

    #user_face_icon {
      display: inline-block;
      background: red;
      width: 50px;
      height: 50px;
      border-radius: 30px;
      position: absolute;
      bottom: 10px;
      left: 14px;
      cursor: pointer;
      overflow: hidden;
    }

    img {
      width: 50px;
      height: 50px;
      border-radius: 10px;
    }

    .content {
      font-size: 20px;
      width: 435px;
      height: 90%;
      overflow: auto;
      padding: 5px;
      word-wrap: break-word;
      text-align: justify;
    }

    .content li {
      margin-top: 10px;
      padding-left: 10px;
      width: 412px;
      display: block;
      clear: both;
      overflow: hidden;
    }

    .content li img {
      float: left;
    }

    .content li span {
      background: #7cfc00;
      padding: 10px;
      border-radius: 10px;
      float: left;
      margin: 6px 10px 0 10px;
      max-width: 310px;
      border: 1px solid #ccc;
      box-shadow: 0 0 3px #ccc;
    }

    .content li img.imgleft {
      float: left;
    }

    .content li img.imgright {
      float: right;
    }

    .content li span.spanleft {
      float: left;
      max-width: 55%;
      background: #fff;
    }

    .content li span.spanright {
      float: right;
      max-width: 55%;
      background: #7cfc00;
    }
  </style>
</head>

<body>
  <div id="container">
    <div class="header">
      <span style="float: left;">知心姐姐</span>
      <span style="float: right;"></span>
    </div>
    <ul class="content">
      <li v-for="(item,index) in imgList">
        <!-- 使用template标签，当item.isMe为true则表示当前消息是我说的，配合v-if标签使用 -->
        <template v-if="item.isMe">
          <img src="./img/right.png" class="imgright"><span class="spanright">{{item.msg}}</span>
        </template>

        <template v-else>
          <img src="./img/left.png" class="imgleft"><span class="spanleft">{{item.msg}}</span>
        </template>

      </li>
    </ul>
    <div class="footer">
      <div id="user_face_icon">
        <img src="./img/right.png" alt="" />
      </div>
      <input id="text" type="text" @keyup.enter="chat" v-model.trim="inputMsg" placeholder="说点什么吧..." />
      <span id="btn" @click="chat">发送</span>
    </div>
  </div>
</body>
<script src="./js/jquery-1.12.4.min.js"></script>
<script src="./js/vue.js"></script>
<script>
  new Vue({
    el: "#container",
    data: {
      inputMsg: "",
      imgList: [

      ]
    },
    methods: {
      // chat()方法用于发送ajax请求, 当用户点击发送按钮或者回车时都会触发chat函数
      chat() {
        // 先判断用户输入的内容不为空
        if (!this.inputMsg) {
          alert("输入信息不能为空!");
          return
        }
        else {
          let myObj = {
            msg: this.inputMsg,
            isMe: true
          };
          // 将数据添加到数据列表中
          var tempMsg = this.inputMsg;
          this.inputMsg = "";
          this.imgList.push(myObj);
        }

        // 将用户的信息以ajax形式发送 
        $.ajax({
          type: 'POST',
          url: 'https://api.ownthink.com/bot',
          data: {
            "spoken": tempMsg,
          },
          // 回调函数中的this指的是当前的ajax,要想使用vue中的this,则应该使用箭头函数
          success: res => {
            // 如果信息获取成功,则将该条回复信息添加到信息列表中
            if (res.message === 'success') {
              let replyInfo = res.data.info.text;
              let replyObj = {
                msg: replyInfo,
                isMe: false
              };
              console.log(replyObj);
              this.imgList.push(replyObj)
              this.inputMsg = "";

              let timerId = setTimeout(() => {
                $(".content").scrollTop(10000);
                clearTimeout(timerId);
              }, 0);
            }
          },
          err(err) {
            console.log(err);

          }
        })
        // 等列表渲染之后再移动页面
        // this.$nextTick(() => {
        //   $(".content").scrollTop(10000);
        // })
      }
    }
  })
</script>

</html>